﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Web;

/// <summary>
/// 封装访问数据库访问过程的类
/// </summary>
sealed internal class StoredProcedure : IDisposable
{
    public string ErrorMessage = "";
    private System.Data.SqlClient.SqlCommand command;

    public StoredProcedure(string SqlText)
    {
        ConfigManager config = new ConfigManager();
        command = new SqlCommand(SqlText, new SqlConnection(config.DALConnectionString));
        command.CommandType = CommandType.Text;
        command.Connection.Open();
    }
    public StoredProcedure(string sprocName, SqlParameter[] parameters)
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
        ConfigManager config = new ConfigManager();
        command = new SqlCommand(sprocName, new SqlConnection(config.DALConnectionString));
        command.CommandType = CommandType.StoredProcedure;
        if (parameters != null)
        {
            foreach (SqlParameter parameter in parameters)
                command.Parameters.Add(parameter);
        }
        command.Connection.Open();
    }

    ///	<summary>
    ///	Dispose of this StoredProcedure.
    /// </summary>
    public void Dispose()
    {
        if (command != null)
        {
            SqlConnection connection = command.Connection;
            Debug.Assert(connection != null);
            command.Dispose();
            command = null;
            connection.Dispose();
        }
    }

    ///	<summary>
    /// 执行存储过程
    ///	<returns>返回一个整型</returns>
    /// </summary>

    public int Run()
    {
        if (command == null)
            throw new ObjectDisposedException(GetType().FullName);
        try
        {
            command.ExecuteNonQuery();
            return 1;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return 0;
        }

    }

    ///	<summary>
    /// 执行存储过程
    ///	<returns>返回一个整型</returns>
    /// </summary>

    public int Run(out int num)
    {
        if (command == null)
            throw new ObjectDisposedException(GetType().FullName);
        try
        {
            num = Convert.ToInt32(command.ExecuteScalar());
            return 1;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            num = 0;
            return 0;
        }

    }

    /// <summary>
    ///	执行存储过程填充一个table.
    /// <param name='dataTable'>
    /// DataTable filled with the results of executing the stored procedure</param>
    /// <returns>
    /// Int32 value returned by the stored procedure</returns>
    /// </summary>

    public int Run(DataTable dataTable)
    {
        if (command == null)
            throw new ObjectDisposedException(GetType().FullName);
        try
        {
            SqlDataAdapter dataAdapter = new SqlDataAdapter();

            dataAdapter.SelectCommand = command;
            dataAdapter.Fill(dataTable);
            return 1;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return 0;
        }
    }
}